<link rel="stylesheet" href="../static/css/style.css">
<link rel="stylesheet" href="../static/css/styleoneadd.css">
<link rel="stylesheet" href="../static/plugins/layui/css/layui.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap.min.css">

<style>
    body{
        background-color: #f2f2f2 !important;
    }
</style>
<!-- 内容主体区域 -->
<div class="layui-fluid-pad" ng-app="paperType">
    <div class="layui-col-md12" ng-controller="paperTypeManage" id="paperTypeManage">
        <div class="layui-card">
            <div class="layui-card-header">问卷类型</div>
            <div class="layui-card-body">
                <div class="layui-row layui-pad15">
                    <form class="layui-form" action="">
                        <div class="layui-col-md12 layui-col-lh">
                            <div class="layui-row layui-col-space15">
                                <div class="layui-col-md2"><input type="text" id="typeName" name="typeName"  placeholder="类型名称" class="form-control"/></div>
                                <div class="layui-col-md3">
                                    <div class="layui-row">
                                        <div class="layui-col-md12 layui-order-flexs">
                                            <input type="text" id="startTime" class="form-control layui-gd-widths" placeholder="时间">
                                            &nbsp;&nbsp;至&nbsp;&nbsp;
                                            <input type="text" id="endTime" class="form-control layui-gd-widths" placeholder="时间">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md1">
                                    <button type="button" class="layui-btn layui-t-h" ng-click="search()">筛选</button>
                                </div>
                                <div class="layui-col-md6 layui-text-right">
                                    <a class="layui-btn layui-t-h" id="addBtn">
                                        <i class="layui-icon" style="font-size:20px;color: #ffffff;">&#xe63c;</i>
                                        新增类型
                                    </a>
                                </div>
                            </div>
                        </div>
                    </form>

                    <!--表格-->
                    <div class="layui-row">
                        <div class="layui-col-md12">
                            <table class="layui-data-zs" id="test3" border="1" style="border: 1px solid #e5e5e5;">
                                <tr class="layui-t-h layui-hg-b" style=" background-color: #34C0E3;color: #ffffff;">
                                    <th>序号</th>
                                    <th>类型名称</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                                <tbody id="paperTypeList">
                                    <tr class="layui-hg-b" ng-repeat="paperType in paperTypeList">
                                        <td><span ng-bind="$index + 1"></span></td>
                                        <td><span ng-bind="paperType.typeName || '--'"></span></td>
                                        <td><span ng-bind="(paperType.createdTime|date:'yyyy-MM-dd') || '--'"></span></td>
                                        <td>
                                            <a href="#" ng-click="showEditWindow(paperType.id)" class="layui-btn-ty layui-zdy-bj">编辑</a>
                                            <a href="#" ng-click="delete(paperType.id)" class="layui-btn-ty layui-dels">删除</a>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            <div ng-show="!paperTypeList || paperTypeList.length == 0" style="margin: 20px 0; text-align: center;">无符合条件数据。</div>
                        </div>
                        <!--分页-->
                        <div class="layui-col-md12 layui-page-top" ng-hide="!paperTypeList || paperTypeList.length == 0">
                            <div id="pagination"></div>
                        </div>
                        <!--分页end-->
                    </div>
                    <!--表格end-->
                </div>
            </div>
        </div>
    </div>
</div>


<script src="../static/js/jquery.min.js"></script>
<script src="../static/plugins/layui/layui.js"></script>
<script src="../static/plugins/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<script src="../static/js/angular.js"></script>
<script src="../static/js/app/consts/consts.js"></script>
<script>
    var app = angular.module('paperType',['ng']);
    let searchUrl = baseUrl + "/exam/paper-type";
    let deleteUrl = baseUrl + "/exam/paper-type";

    let currentPageNumber=1;
    let currentPageSize=10;
    let currentTotal=0;
    let currentResultInfo=[];
    app.controller('paperTypeManage',function($scope, $http){
        //页面加载时执行的函数
        $(function() {
            // 初始化数据
            $http({
                method: 'GET',
                params: {
                    pageNumber: currentPageNumber,
                    pageSize: currentPageSize,
                    sort: "",
                    desc: ""
                },
                url: searchUrl
            }).then(function successCallback(response){
                if (response && response.data && response.data.state == 1) {
                    let resultInfo = response.data.data;
                    updatePagination(resultInfo);
                    if (resultInfo) {
                        $scope.paperTypeList = resultInfo.data;
                    }
                    if (response.data.msg) {
                        layer.msg(response.data.msg)
                    }
                }
            }, function errorCallback(response) {
                console.log("err");
            });
            refreshPagination();
        });
        // 筛选按钮事件
        $scope.search = function(goPageNumber) {
            // 查找时到第一页
            if (!goPageNumber) {
                currentPageNumber = 1;
            }
            var data = getSearchParam();
            // if (data.param == ""&& data.startTime == "") {
            //     layer.msg("请输入搜索内容或时间"); return;
            // }
            $http({
                method: 'GET',
                url: searchUrl,
                params: data
            }).then(function successCallback(response) {
                if (response && response.data && response.data.state == 1) {
                    let resultInfo = response.data.data;
                    updatePagination(resultInfo);
                    if (resultInfo) {
                        $scope.paperTypeList = resultInfo.data;
                    }
                    if (response.data.msg) {
                        layer.msg(response.data.msg)
                    }
                }
            }, function errorCallback(response) {
                console.log("err");
            });
        };
        $scope.showEditWindow = function (paperTypeId) {
            var strU = 'addType.html?processingType=2&processingId=' + paperTypeId;
            layer.open({
                type: 2,
                title: '问卷类型编辑',
                area: ['480px', '180px'],
                shadeClose: true, //开启遮罩关闭
                content: strU
            });
        };
        $scope.delete = function(paperTypeId) {
            layer.open({
                area: '300px;',
                title: '确认',
                content: '确认删除？',
                btn: ['确认', '取消'],
                closeBtn: false,
                resize: false,
                yes: function (index, layero) {
                    $http({
                        method: 'DELETE',
                        url: deleteUrl + "?paperTypeId=" + paperTypeId,
                    }).then(function successCallback(response) {
                        if (response && response.data && response.data.state == 1) {
                            $scope.search(currentPageNumber);
                            layer.msg("删除成功");
                        } else {
                            layer.msg("删除失败");
                        }
                    }, function errorCallback(response) {
                        layer.msg("删除失败");
                    });
                    layer.close(index);
                }
            });
        };
    });

    layui.use(['form','laypage','laydate','table','layer'], function(){
        var form = layui.form;
        var laydate = layui.laydate;
        var laypage = layui.laypage;
        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
        // 日期
        laydate.render({
            elem: '#startTime' //指定元素
            ,theme: '#34C0E3'
        });
        laydate.render({
            elem: '#endTime' //指定元素
            ,theme: '#34C0E3'
        });
        //增加选项
        $(function () {
            $('#addBtn').on('click',function () {
                var strU = 'addType.html?processingType=1';
                layer.open({
                    type: 2,
                    title: '新增问卷类型',
                    area: ['480px', '180px'],
                    shadeClose: true, //开启遮罩关闭
                    content: strU
                });
            });
        });

    });

    // 页面刷新
    function refreshPagination() {
        layui.use(['laypage', 'layer'], function() {
            var laypage = layui.laypage;
            //分页
            laypage.render({
                elem: 'pagination'
                ,theme: '#34C0E3'
                ,count: currentTotal
                ,curr: currentPageNumber
                ,limit: currentPageSize
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                ,jump: function(obj, first) {
                    if (!first) {
                        currentPageNumber = obj.curr;
                        currentPageSize = obj.limit;
                        doSearch(currentPageNumber);
                    }
                }
            });
        });
    }

    function doSearch(currentPageNumber) {
        let scope = getAngularScope();
        scope.search(currentPageNumber);
    }

    // 获取angular scope
    function getAngularScope() {
        return angular.element(paperTypeManage).scope();
    }

    // 获取查询条件
    function getSearchParam() {
        var data = {
            typeName: $("#typeName").val(),
            pageNumber: currentPageNumber,
            pageSize: currentPageSize,
            sort: "",
            desc: ""
        };
        if ($("#startTime").val()) {
            data.startTime = $("#startTime").val();
        }
        if ($("#endTime").val()) {
            data.endTime = $("#endTime").val();
        }
        return data;
    }

    // 更新页码信息
    function updatePagination(resultInfo) {
        currentPageNumber = resultInfo.pageNumber || 1;
        currentPageSize = resultInfo.pageSize || 10;
        currentTotal = resultInfo.total || 0;
        currentResultInfo = resultInfo;
        refreshPagination();
    }
</script>




